package com.homedo.microservice.open.service.gateway.manager.web.remote.api.impl;

import com.google.common.collect.Lists;
import com.homedo.common.bean.data.RequestData;
import com.homedo.common.bean.data.ResponseData;
import com.homedo.common.bean.vo.Order;
import com.homedo.common.bean.vo.Pagination;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.manager.web.api.IRouteVersionServiceRemote;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RouteVersionAddAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RouteVersionPageAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RouteVersionUpdateAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.RouteVersionAResp;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.RouteVersionPageAResp;
import com.homedo.microservice.open.service.gateway.manager.web.convent.CommonConvent;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.RouteVersion;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.RouteVersionEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.query.RouteVersionQuery;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.service.RouteVersionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RouteVersionServiceRemoteImpl implements IRouteVersionServiceRemote {
    @Autowired
    private RouteVersionService routeVersionService;

    @Override
    public ResponseData<RouteVersionPageAResp> list(RouteVersionPageAReq routeVersionPageAReq) {
        ResponseData<RouteVersionPageAResp> responseData = new ResponseData<>();

        RouteVersionQuery routeVersionQuery = new RouteVersionQuery();
        routeVersionQuery.setRouteIds(Lists.newArrayList(routeVersionPageAReq.getRouteId()));

        Pagination<RouteVersionEnhanced> pagination = new Pagination<>(1, Integer.MAX_VALUE, false);
        pagination.addOrder(new Order(RouteVersion.COLUMN_STATUS, Order.ORDER_DESC), RouteVersion.class)
                .addOrder(new Order(RouteVersion.COLUMN_CREATE_TIME, Order.ORDER_DESC), RouteVersion.class);


        pagination = routeVersionService.getPagination(routeVersionQuery, pagination, null);

        RouteVersionPageAResp routeVersionPageAResp = new RouteVersionPageAResp();
        routeVersionPageAResp.setCount(pagination.getCount());
        routeVersionPageAResp.setPage(pagination.getIndex());
        routeVersionPageAResp.setSize(pagination.getSize());

        if (Detect.notEmpty(pagination.getItems())) {
            routeVersionPageAResp.setItems(CommonConvent.convent2RouteVersionAResps(pagination.getItems()));
        }

        responseData.setData(routeVersionPageAResp);

        return responseData;
    }

    @Override
    public ResponseData<RouteVersionAResp> detail(@PathVariable("id") String id) {
        ResponseData<RouteVersionAResp> responseData = new ResponseData<>();

        RouteVersionEnhanced routeVersionEnhanced = routeVersionService.getEnhanced(id, null);

        responseData.setData(CommonConvent.convent2RouteVersionAResp(routeVersionEnhanced));

        return responseData;
    }

    @Override
    public ResponseData update(@RequestBody RequestData<RouteVersionUpdateAReq> requestData) {
        ResponseData responseData = new ResponseData();

        RouteVersionUpdateAReq routeVersionUpdateAReq = requestData.getRequest();

        routeVersionService.updateRouteVersion(routeVersionUpdateAReq.getId(), routeVersionUpdateAReq.getVersion(),
                routeVersionUpdateAReq.getServiceId(), routeVersionUpdateAReq.getComment(),
                routeVersionUpdateAReq.getStatus(), null);

        return responseData;
    }

    @Override
    public ResponseData<RouteVersionAResp> add(@RequestBody RequestData<RouteVersionAddAReq> requestData) {
        ResponseData<RouteVersionAResp> responseData = new ResponseData<>();

        RouteVersion routeVersion = new RouteVersion();
        BeanUtils.copyProperties(requestData.getRequest(), routeVersion);

        routeVersion = routeVersionService.addRouteVersion(routeVersion, null);

        responseData.setData(CommonConvent.convent2RouteVersionAResp(routeVersionService.convent2Enhanced(routeVersion)));

        return responseData;
    }

    @Override
    public ResponseData delete(@PathVariable("id") String id) {
        ResponseData responseData = new ResponseData();

        routeVersionService.deleteById(id, null);

        return responseData;
    }
}
